package com.example.schoolhelp.fragment.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.schoolhelp.R;
import com.example.schoolhelp.activity.RewordDetailActivity;
import com.example.schoolhelp.adapter.MyItemDecoration;
import com.example.schoolhelp.adapter.PostRecycleAdapter;
import com.example.schoolhelp.base.BaseRecyclerAdapter;
import com.example.schoolhelp.bean.PostBean;
import com.example.schoolhelp.view.MyToast;

import java.util.ArrayList;

/**
 * Created by user on 2017/7/20.
 */

public class PostFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    View layout;
    Context context;
    SwipeRefreshLayout swipeRefreshLayout;
    RecyclerView recycleView;
    PostRecycleAdapter adapter;
    MyHandler handler;
    ArrayList<PostBean> postList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.fragment_post, container, false);
        context = getActivity();
        handler = new MyHandler();
        initData();
        initView();
        initViewData();
        initEvent();
        return layout;
    }

    private void initData() {
        for (int i = 0; i < 20; i++) {
            PostBean postBean = new PostBean("隔壁老王", "和我在成都的街头走一走", "2", "4.5");
            postList.add(postBean);
        }
    }

    private void initView() {
        swipeRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipeRefreshLayout);
        recycleView = (RecyclerView) layout.findViewById(R.id.post_recycleView);
    }

    private void initViewData() {
        recycleView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recycleView.setItemAnimator(new DefaultItemAnimator());
        recycleView.addItemDecoration(new MyItemDecoration(context, true));
//        adapter = new PostRecycleViewAdapter(null, context);

        View headView = LayoutInflater.from(context).inflate(R.layout.item_post_head_layout, null);
        adapter = new PostRecycleAdapter(postList, context);
        adapter.setHeaderView(headView);
        recycleView.setAdapter(adapter);
    }

    private void initEvent() {

        swipeRefreshLayout.setOnRefreshListener(this);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data, View itemView) {
//                MyToast.showToast(context,"position"+position);
                PostBean item = (PostBean) data;
                Intent intent = new Intent(context, RewordDetailActivity.class);
                intent.putExtra("data", item);
                intent.putExtra("position", position);
                ActivityOptionsCompat aoc = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
                        new Pair<View, String>(itemView.findViewById(R.id.item_post_iv_pic), "iv"),
                        new Pair<View, String>(itemView.findViewById(R.id.item_post_tv_desc), "tv")
                );
                ActivityCompat.startActivity(context, intent, aoc.toBundle());
            }
        });
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 2000);
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    }
}
